<?php

namespace App\Http\Controllers\Admin\Statistics;

use App\Exports\ProductLogExport;
use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\Order;
use App\Models\Product;
use App\Models\ProductLog;
use App\Models\User;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\Statistics\ProductLogTransformer;
use App\Transformers\Product\ProductTransformer;
use Carbon\Carbon;
use Dingo\Api\Http\Response;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Ramsey\Collection\Set;
use function DI\get;

class ProductLogController extends Controller
{
    use AdminTraits;

    public $model = ProductLog::class;
    public $transformer = ProductLogTransformer::class;

    /**
     * 商品统计的头部
     */
    public function top(Request $request)
    {

        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        $start = $request->input('start') ?? '1997-1-1';
        $end = $request->input('end') ?? '2222-1-1';

        //商品浏览量
        $count = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //付款人数
        $product_log = Order::query()->pimp()
            ->where('paid', 1)
            ->with('user:id')
            ->whereBetween('created_at', [$start, $end])
            ->count();


        //商品访客数
        $visitor_num = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->count();

        if ($product_log == 0 && $visitor_num == 0) {
            $Payment = 0;
        } else {
            //访客-支付转化率
            $Payment = sprintf("%01.2f", ($product_log / $visitor_num) * 100) . '%';
        }

        //加购件数
        $cart_num = Cart::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //下单件数
        $order_num = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //支付件数
        $pay_num = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('paid', 1)
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //支付金额
        $pay_price = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('paid', 1)
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->sum('pay_price');

        //成本金额
        $cost_price = Order::query()->pimp()
            ->where('paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->sum('cost');

        //退款金额
        $refund_price = Order::query()->pimp()
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->sum('refund_price');

        //退款件数
        $refund_num = Order::query()->pimp()
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->count();

        $data = [

            'name1' => '商品浏览量',
            'visit_num' => $count,

            'name2' => '商品访客数',
            'visitor_num' => $visitor_num,

            'name3' => '加购件数',
            'cart_num' => $cart_num,

            'name4' => '下单件数',
            'order_num' => $order_num,

            'name5' => '支付件数',
            'pay_num' => $pay_num,

            'name6' => '支付金额',
            'pay_price' => $pay_price,

            'name7' => '成本金额',
            'cost_price' => $cost_price,

            'name8' => '退款金额',
            'refund_price' => $refund_price,

            'name9' => '退款件数',
            'refund_num' => $refund_num,

            'name10' => '访客-支付转化率',
            'Payment' => $Payment,

        ];

        return $this->response->array($data);
    }


    /**
     * 统计列表
     */
    public function productlog(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        $start = $request->input('start') ?? '1997-1-1';
        $end = $request->input('end') ?? '2222-1-1';


        $data = $this->getQuery()->pimp()
            ->GroupBy('product_id')
            ->OrderBy('product_id', 'desc')
            ->with('product:id,image,product_name')
            ->whereBetween('created_at',[$start,$end])
            ->selectRaw('product_id,
                            sum(visit_num) as visit_num,
                            sum(cost_price) as cost_price,
                            sum(cart_num) as cart_num,
                            sum(order_num) as order_num,
                            sum(pay_num) as pay_num,
                            sum(pay_price) as pay_price,
                            sum(collect_num) as collect_num'
            )
            ->get();

        return $this->response->collection($data, new ProductLogTransformer());

    }

    public function growth(Request $request)
    {
        //今年的环比增长的时间
        $this_year = [
            'data' => Carbon::today()->subMonths(8)->subDays(21)
                ->toDateTimeString(),
            'data1' => Carbon::today()->addMonths(3)->addDays(9)
                ->toDateTimeString(),
        ];
        //上一年的环比增长的时间
        $last_year = [
            'data' => Carbon::today()->subYears()->subMonths(8)->subDays(21)
                ->toDateTimeString(),
            'data1' => Carbon::today()->subYears()->addMonths(3)->addDays(9)
                ->toDateTimeString(),
        ];
        //本期的
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        $start = $request->input('start') ?? '1997-1-1';
        $end = $request->input('end') ?? '2222-1-1';

        //商品浏览量
        $count = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //付款人数
        $product_log = Order::query()->pimp()
            ->where('paid', 1)
            ->with('user:id')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();


        //商品访客数
        $visitor_num = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        if ($product_log == 0 && $visitor_num == 0) {
            $Payment = 0;
        } else {
            //访客-支付转化率
            $Payment = sprintf("%01.2f", $product_log / $visitor_num);
        }

        //加购件数
        $cart_num = Cart::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //下单件数
        $order_num = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //支付件数
        $pay_num = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('paid', 1)
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //支付金额
        $pay_price = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('paid', 1)
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->sum('pay_price');

        //成本金额
        $cost_price = Order::query()->pimp()
            ->where('paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->sum('cost');

        //退款金额
        $refund_price = Order::query()->pimp()
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->sum('refund_price');

        //退款件数
        $refund_num = Order::query()->pimp()
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //上一年
        //商品浏览量
        $count1 = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //付款人数
        $product_log1 = Order::query()->pimp()
            ->where('paid', 1)
            ->with('user:id')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();


        //商品访客数
        $visitor_num1 = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        if ($visitor_num1 == 0) {
            $Payment1 = 0;
        } else {
            //访客-支付转化率
            $Payment1 = round($product_log1 / $visitor_num1, 2);
        }

        //加购件数
        $cart_num1 = Cart::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //下单件数
        $order_num1 = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //支付件数
        $pay_num1 = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('paid', 1)
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //支付金额
        $pay_price1 = Order::query()->pimp()
            ->where('type', 'pinkOrder')
            ->where('paid', 1)
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->sum('pay_price');

        //成本金额
        $cost_price1 = Order::query()->pimp()
            ->where('paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->sum('cost');

        //退款金额
        $refund_price1 = Order::query()->pimp()
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->sum('refund_price');

        //退款件数
        $refund_num1 = Order::query()->pimp()
            ->where('refund_status', 'refunded')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //环比增长率

        if ($count1 == 0) {
            $count2 = 0 . '%';
        } else {
            $count2 = round(($count - $count1) / $count1, 2) * 100 . '%';
        }

        if ($visitor_num1 == 0) {
            $visitor_num2 = 0 . '%';
        } else {
            $visitor_num2 = round(($visitor_num - $visitor_num1) / $visitor_num1, 2) * 100 . '%';
        }

        if ($cart_num1 == 0) {
            $cart_num2 = 0 . '%';
        } else {
            $cart_num2 = round(($cart_num - $cart_num1) / $cart_num1, 2) * 100 . '%';
        }


        if ($order_num1 == 0) {
            $order_num2 = 0 . '%';
        } else {
            $order_num2 = round(($order_num - $order_num1) / $order_num1, 2) * 100 . '%';
        }

        if ($pay_num1 == 0) {
            $pay_num2 = 0 . '%';
        } else {
            $pay_num2 = round(($pay_num - $pay_num1) / $pay_num1, 2) * 100 . '%';
        }

        if ($pay_price1 == 0) {
            $pay_price2 = 0 . '%';
        } else {
            $pay_price2 = round(($pay_price - $pay_price1) / $pay_price1, 2) * 100 . '%';
        }

        if ($cost_price1 == 0) {
            $cost_price2 = 0 . '%';
        } else {
            $cost_price2 = round(($cost_price - $cost_price1) / $cost_price1, 2) * 100 . '%';
        }

        if ($refund_price1 == 0) {
            $refund_price2 = 0 . '%';
        } else {
            $refund_price2 = round(($refund_price - $refund_price1) / $refund_price1, 2) * 100 . '%';
        }

        if ($refund_num1 == 0) {
            $refund_num2 = 0 . '%';
        } else {
            $refund_num2 = round(($refund_num - $refund_num1) / $refund_num1, 2) * 100 . '%';
        }

        if ($Payment1 == 0) {
            $Payment2 = 0 . '%';
        } else {
            $Payment2 = round(($Payment - $Payment1) / $Payment1, 2) * 100 . '%';
        }

        $data = [

            'visit_num' => $count2,

            'visitor_num' => $visitor_num2,

            'cart_num' => $cart_num2,

            'order_num' => $order_num2,

            'pay_num' => $pay_num2,

            'pay_price' => $pay_price2,

            'cost_price' => $cost_price2,

            'refund_price' => $refund_price2,

            'refund_num' => $refund_num2,

            'Payment' => $Payment2,
        ];
        return $this->response->array($data);
    }

    public function graphical()
    {
        // 返回30天时间
        for ($i = 0; $i < 30; $i++) {
            $time[] = Carbon::now()->subDays($i)->toDateString();
        }

        //返回30天商品数据
        for ($i = 0; $i < 30; $i++) {
            $start_time = Carbon::now()->startOfDay()->subDays($i);
            $end_time = Carbon::now()->endOfDay()->subDays($i);
            $data[$i] = $this->getQuery()
                ->whereBetween('created_at', [$start_time, $end_time])
                ->selectRaw('SUM(visit_num) as visit_num,
                            SUM(visitor_num) as visitor_num,
                            SUM(pay_price) as pay_price,
                            SUM(refund_price) as refund_price'
                )->get();
        }

        //改变数据格式
          foreach ($data as $k => $v){
               foreach ($v as $a => $b){
                   $arr[0]['data'][] = $b['visit_num'];
                   $arr[1]['data'][] = $b['visitor_num'];
                   $arr[2]['data'][] = $b['pay_price'];
                   $arr[3]['data'][] = $b['refund_price'];
               }
          }

        $arr[0]['name'] = '商品浏览量';
        $arr[0]['type'] = 'line';
        $arr[1]['name'] = '商品访客量';
        $arr[1]['type'] = 'line';
        $arr[2]['name'] = '支付金额';
        $arr[2]['type'] = 'bar';
        $arr[3]['name'] = '退款金额';
        $arr[3]['type'] = 'bar';
        $array['series'] = $arr;
        $array['xAxis'] = $time;
        return $this->response->array($array);

    }

}

